package cn.tedu.ivos.audit.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.dto.AuditQuery;
import cn.tedu.ivos.audit.pojo.dto.AuditSaveParam;
import cn.tedu.ivos.audit.pojo.entity.Audit;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

@Service
public class AuditServiceImpl implements AuditService {
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ApplicationMapper applicationMapper;
    @Override
    public void insertAudit(Application application) {
        //根据当前申请单的信息，进行新增
        //获取当前申请表单的上级
        List<Long> auditUserIdList = application.getAuditUserIdList();
        for (int i = 0; i < auditUserIdList.size(); i++) {
            //获取当前审批人的id
            Long auditUserId = auditUserIdList.get(i);
            //准备审批对象
            Audit audit = new Audit();
            audit.setApplicationId(application.getId());
            audit.setAuditUserId(auditUserId);
            //当i==0时，表示是第一个审批信息，设置状态为“待我审核”
            if(i==0){
                audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
            }else {
                //其他审批信息，设置为“待他人审核”
                audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            }
            //设置审批顺序，数字越小越先审批  0 上级  1 上上级
            audit.setAuditSort(i);
            //入库
            auditMapper.insert(audit);
        }
    }

    @Override
    public List<AuditVO> selectAudit(AuditQuery auditQuery) {
        List<AuditVO> auditVOList =  auditMapper.selectAudit(auditQuery);
        for (AuditVO auditVO : auditVOList) {
            //准备审批人的数据
            auditUserList(auditVO);
        }
        return auditVOList;
    }

    @Override
    public void updateAudit(AuditSaveParam auditSaveParam) {
        //将auditSaveParam赋值到audit对象中
        Audit audit = new Audit();
        BeanUtils.copyProperties(auditSaveParam,audit);
        //准备一个Application对象
        Application application = new Application();
        application.setId(audit.getApplicationId());
        //前端点击通过，传到后端的状态码是30
        //判断是否为30
        if(audit.getAuditStatus().equals(AuditStatusEnum.AUDITED.getCode())){
            //是30，更新入库库，将当前登陆人的审批单的状态修改为已审核
            auditMapper.update(audit);
            //继续查询当前申请工单下的其他审批信息
            AuditQuery auditQuery = new AuditQuery();
            auditQuery.setApplicationId(audit.getApplicationId());
            Integer count = auditMapper.selectAuditCount(auditQuery);
            //判断count是否大于0，如果大于0就是有其他审批信息
            if(count>0){
                //有下一条审批数据，修改当前审批人的状态为已审核，下一个审批人的状态为待我审核，工单为审核中
                //还需修改审核的顺序+1
                auditQuery.setAuditSort(audit.getAuditSort()+1);
                //查询下一条审核信息
                List<AuditVO> auditVOS = auditMapper.selectAudit(auditQuery);
                //判断集合不能空，至少包含一条数据
                if(auditVOS!=null && auditVOS.size()>0){
                    //取出集合中的第一条数据
                    AuditVO auditVO = auditVOS.get(0);
                    Audit newAudit =  new Audit();
                    //设置id
                    newAudit.setId(auditVO.getId());
                    //设置状态为待我审核
                    newAudit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
                    auditMapper.update(newAudit);
                }
                //设置工单为审核中
                application.setStatus(ApplicationStatusEnum.AUDIT.getCode());
                applicationMapper.update(application);
            }else {
                //没有其他要审核的数据
                //设置申请工单为已审核
                application.setStatus(ApplicationStatusEnum.AUDITED.getCode());
                applicationMapper.update(application);
            }
        }else if(audit.getAuditStatus().equals(AuditStatusEnum.REJECT.getCode())){//判断状态是否为40
            auditMapper.update(audit);
            AuditQuery auditQuery = new AuditQuery();
            auditQuery.setApplicationId(audit.getApplicationId());
            //根据申请单id查询，是否有剩余的审核信息
            List<AuditVO> auditVOS = auditMapper.selectAudit(auditQuery);
            //判断是否为空，如果不为空删除
            if(auditVOS!=null && auditVOS.size()>0){
                //遍历审核信息，进行删除
                for (AuditVO auditVO : auditVOS) {
                    //删除未审批的数据
                    if(auditVO.getAuditStatus().equals(AuditStatusEnum.MY_PENDING.getCode())){
                        auditMapper.deleteById(auditVO.getId());
                    }
                }
            }
            //设置申请单的状态为驳回，同时需要设置驳回原因
            application.setStatus(ApplicationStatusEnum.REJECT.getCode());
            application.setRejectReason(auditSaveParam.getRejectReason());
            //更新数据
            applicationMapper.update(application);
        }
    }

    private void auditUserList(AuditVO auditVO) {
        //准备两个集合，存储审批人的id和姓名
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        //根据applicationId查询审批人的信息
        List<AuditVO> auditVOList = auditMapper.selectAuditByApplicationId(auditVO.getApplicationId());
        //遍历auditVOList，取出每一个auditVO对象
        for (int i = 0; i < auditVOList.size(); i++) {
            //取出当前审批单的id，赋值到auditUserIdList中
            Long auditUserId = auditVOList.get(i).getAuditUserId();
            auditUserIdList.add(auditUserId);
            //根据审批单的id查询用户数据
            UserVO userVO = userMapper.selectAuditById(auditUserId);
            //将用户的名称赋值到auditUsernameList中
            auditUsernameList.add(userVO.getUsername());
        }
        //将准备好的auditUserIdList赋值到applicationVO中
        auditVO.setAuditUserIdList(auditUserIdList);
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String username : auditUsernameList) {
            stringJoiner.add(username);
        }
        auditVO.setAuditUsernameList(stringJoiner.toString());
    }
}
